package com.dtf.admin.menu.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dtf.admin.menu.form.SystemMenuForm;
import com.dtf.admin.menu.service.SystemMenuService;
import com.dtf.admin.menu.vo.SystemMenu;
import com.dtf.core.common.utils.Const;

@Controller
@RequestMapping("/admin/menu")
@SuppressWarnings("all")
public class SystemMenuController {
	@Resource
	private SystemMenuService menuService;

	@ResponseBody
	@RequestMapping("addMenu")
	public Map addMenu(HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes, SystemMenuForm form) {
		SystemMenu systemMenu = form.getSystemMenu();
		
		SystemMenu parentMenu = new SystemMenu();
		parentMenu.setMenu_id(systemMenu.getParent_id());
		
		parentMenu = (SystemMenu) menuService.get(parentMenu);
		systemMenu.setLevel(parentMenu.getLevel()+1);
		String id = Const.getUUID();
		systemMenu.setMenu_id(id);
		menuService.add(systemMenu);
		
		String menu_path = parentMenu.getMenu_path();
		menu_path = StringUtils.isEmpty(menu_path) ? parentMenu.getMenu_id()+"."+id : (menu_path + "." +id);
		
		systemMenu.setMenu_path(menu_path);
		menuService.edit(systemMenu);
		
		Map obj = new HashMap();
		obj.put("msg", "新增成功");
		obj.put("obj", systemMenu);
		return obj;
	}
	
	@ResponseBody
	@RequestMapping("updateMenu")
	public Map updateMenu(HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes, SystemMenuForm form) {
		SystemMenu systemMenu = form.getSystemMenu();
		menuService.edit(systemMenu);
		Map obj = new HashMap();
		obj.put("msg", "更新成功");
		obj.put("obj", systemMenu);
		return obj;
	}
	
	@ResponseBody
	@RequestMapping("deleteMenu")
	public Map deleteMenu(HttpServletRequest request,
			HttpServletResponse response,
			RedirectAttributes redirectAttributes, String menu_id) {
		menuService.deleteMenu(menu_id);
		Map obj = new HashMap();
		obj.put("msg", "删除成功");
		return obj;
	}
	
	@ResponseBody
	@RequestMapping("loadMenuForID")
	public SystemMenu loadMenuForID(HttpServletRequest request,
			HttpServletResponse response,String menu_id) {
		SystemMenu menu = new SystemMenu();
		menu.setMenu_id(menu_id);
		return (SystemMenu) menuService.get(menu);
	}
	
	
	@ResponseBody
	@RequestMapping("loadAllMenuBtn")
	public List loadAllMenuBtn(HttpServletRequest request,
			HttpServletResponse response, SystemMenuForm form) {
		
		List<Map<String, Object>> list = menuService.loadAllMenuBtn(null);
		
		//如果只是页面的情况下，更改页面的图标
		for (Map<String, Object> map : list) {
			String type = Const.getStrValue(map, "type");
			String menuId = Const.getStrValue(map, "id");
			//默认展开全部菜单
			map.put("open", "true");
			
			//按钮图标
			if ("btn".equals(type)) {
				map.put("icon", "/ztree/css/zTreeStyle/img/diy/10.png");
				continue;
			}
			
			if ("-1".equals(menuId)) {
				continue;
			}
			
			for (int i = 0; i < list.size(); i++) {
				Map map2 = list.get(i);
				String type2 = Const.getStrValue(map2, "type");
				String pid = Const.getStrValue(map2, "pid");
				if(i == list.size()-1){
					if("btn".equals(type2)){
						map.put("icon", "/ztree/css/zTreeStyle/img/diy/13.png");
					}else if(!menuId.equals(pid)){
						map.put("icon", "/ztree/css/zTreeStyle/img/diy/13.png");
					}
				}
				if(menuId.equals(pid) && "menu".equals(type2)){
					break;
				}
				
			}
		}
		return list;
	}
}
